package com.example.wuyeguanli.tools;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.AnnouncementItem;

import java.util.List;

// 公告项适配器
public class AnnouncementAdapter extends RecyclerView.Adapter<AnnouncementAdapter.AnnouncementViewHolder>{
    private List<AnnouncementItem> itemList;

    public AnnouncementAdapter(List<AnnouncementItem> itemList) {
        this.itemList = itemList;
    }

    // ViewHolder
    public static class AnnouncementViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;
        public TextView contextTextView;
        public TextView timeTextView;
        public ImageView imageView;

        public AnnouncementViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.announcement_title);
            contextTextView = itemView.findViewById(R.id.announcement_text);
            timeTextView = itemView.findViewById(R.id.announcement_time);
            imageView=itemView.findViewById(R.id.picture);
        }
    }

    @NonNull
    @Override
    public AnnouncementViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.announcement_windows, parent, false);
        return new AnnouncementViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull AnnouncementViewHolder holder, int position) {
        AnnouncementItem currentItem = itemList.get(position);
        holder.titleTextView.setText(currentItem.getTitle());
        holder.contextTextView.setText(currentItem.getContext());
        holder.timeTextView.setText(currentItem.getTime());
        holder.imageView.setImageResource(currentItem.getImage());
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
}
